
const KEY = "goods";

export const getLocalTotal =()=>{
    const getLocalObjInfo = getLocalObj(); 
    let totalCount = 0;
    for(const key in getLocalObjInfo){
        totalCount += getLocalObjInfo[key];
    }
    return totalCount;
}

export const getLocalObj=()=>{
    return JSON.parse(localStorage.getItem(KEY)||'{}');
}

export const addLocalGoods = (goods)=>{
    // console.log(goods);
    const getLocalObjInfo = getLocalObj();
    // console.log(getLocalObjInfo);


    if(getLocalObjInfo[goods.id]){
        getLocalObjInfo[goods.id]+=goods.quantity;
    }else{
        getLocalObjInfo[goods.id]=goods.quantity;
    }
    localStorage.setItem(KEY,JSON.stringify(getLocalObjInfo));
    
    return getLocalTotal();
}


export const updateLocalGoods = (goods) => {
    const getLocalObjInfo = getLocalObj();
    getLocalObjInfo[goods.goodsId]=goods.count;
    localStorage.setItem(KEY,JSON.stringify(getLocalObjInfo));
    return getLocalTotal();
}

export const deleteLocalGoods = (goodsId)=>{
    // console.log(goodsid);
    const getLocalObjInfo = getLocalObj();
    delete getLocalObjInfo[goodsId];
    localStorage.setItem(KEY,JSON.stringify(getLocalObjInfo));
    return getLocalTotal();
}
